#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QDebug>
#include "dirmodel.h"
#include <QTableView>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete model;
    delete ui;

}

//打开原始目录
void MainWindow::on_btnSrcDir_clicked()
{
    srcDirPath = QFileDialog::getExistingDirectory(this,tr("原始目录")
                                                   ,srcDirPath);
    if(srcDirPath=="") return;

    ui->lblSrcDir->setText(srcDirPath);

    model->SetRootDir(srcDirPath);
    ui->tbView->setModel(NULL);

    ui->tbView->setModel(model);
    ui->tbView->resizeColumnsToContents(); //根据内容自动调整列宽
    ui->tbView->resizeRowsToContents(); //根据内容自动调整行高
}

//打开复制到的目录
void MainWindow::on_btnTargetDir_clicked()
{
    targetDirPath = QFileDialog::getExistingDirectory(this,
                                                      tr("复制到路径"),
                                                      targetDirPath);
    ui->lblTargetDir->setText(targetDirPath);
}

//复制文件
void MainWindow::on_btnCopy_clicked()
{
  QFileInfoList list =  model->getSelectFiles();
  int success=0;
  int fail=0;
  foreach (QFileInfo f, list) {

     bool isok =QFile::copy(f.absoluteFilePath(),f.absoluteFilePath().replace(srcDirPath,targetDirPath));
     isok==true?success++:fail++;
  }
   QMessageBox::information(this,"复制结果",QString("成功复制%1个文件,失败%2个文件").arg(success).arg(fail));
}
